<template>
	<view class="bigBox" v-if="goods_info.goods_name">
		<!-- 轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="2000" :duration="1000">
			<swiper-item v-for="(item,index) in goods_info.pics" :key="index">
				<image :src="item.pics_big" mode="" @click="preview(index)"></image>
			</swiper-item>

		</swiper>
		<!-- 商品信息区域 -->
		<view class="good-info-box">
			<!-- 价格区域 -->
			<view class="price">
				￥{{goods_info.goods_price}}
			</view>
			<!-- 商品描述 -->
			<view class="goods-info-body">

				<view class="goods-name">
					{{goods_info.goods_name}}
				</view>
				<!-- 收藏区域 -->
				<view class="favi">

					<uni-icons type="star" size="30"></uni-icons>

					<text>收藏</text>
				</view>
			</view>

			<!-- 运费区域 -->
			<view class="transportPrice">
				快递,免运费
			</view>
		</view>
		<!-- 商品详情数据 -->
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		<!-- 底部导航栏 -->
		<view class="goods_nav">
			<uni-goods-nav :fill="true" :options="options" :button-group="buttonGroup" @click="onclick"
				@buttonClick="buttonClick"></uni-goods-nav>
		</view>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations,
		mapGetters
	} from 'vuex'

	export default {
		watch: {
			// 对象的形式监听
			total: {
				handler(newVal) {
					const findResult = this.options.find((x) => x.text === '购物车')
					if (findResult) {
						findResult.info = newVal
					}
				},
				// 是否页面初次加载完毕后立即调用
				immediate: true
			}
			// 监听total值的变化
			// total(newVal) {
			// 	const findResult = this.options.find((x) => x.text === '购物车')
			// 	if (findResult) {
			// 		findResult.info = newVal
			// 	}
			// }
		},
		computed: {
			...mapState('my_cart', ['cart']),
			...mapGetters('my_cart', ['total'])
		},

		data() {
			return {
				goods_info: {

				},
				options: [{
					icon: 'shop',
					text: '店铺'
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
					text: '加入购物车',
					backgroundColor: '#ff3714',
					color: '#fff'
				}, {
					text: '立即购买',
					backgroundColor: '#ffa200',
					color: '#fff'
				}]
			}

		},
		onLoad(options) {
			const goods_id = options.goods_id
			this.getGoodsDetail(goods_id)
		},
		methods: {
			...mapMutations('my_cart', ['addToCart']),


			async getGoodsDetail(goods_id) {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/goods/detail', {
					goods_id
				})
				//.webp在ios设备上无法正常显示
				this.goods_info.goods_introduce = res.message.goods_introduce.replace(/<img/g,
					'<img style="display:block;"').replace(/webp/g, "jpg")

				this.goods_info = res.message
			},
			preview(index) {
				// 图片预览功能
				uni.previewImage({
					//所有图片的url地址数组
					urls: this.goods_info.pics.map(x => x.pics_big),
					// 预览图片的索引
					current: index
				})

			},
			// 点击购物车图标跳转到购物车页面
			onclick(e) {
				if (e.content.text === '购物车')
					uni.switchTab({
						url: '/pages/cart/cart'
					})
			},
			// 点击加入购物车
			buttonClick(e) {
				if (e.content.text === '加入购物车') {
					// this.options.info++
					//组织商品信息
					const goods = {
						goods_id: this.goods_info.goods_id,
						goods_name: this.goods_info.goods_name,
						goods_price: this.goods_info.goods_price,
						goods_count: 1,
						goods_small_logo: this.goods_info.goods_small_logo,
						goods_state: true

					}

					this.addToCart(goods)
				}


			},
			// 点击跳转到购买页面
		}
	}
</script>

<style lang="scss">
	.bigBox {
		padding-bottom: 50px;
	}

	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.good-info-box {
		padding: 10px;
		padding-right: 0;

		.price {
			color: red;
			font-size: 20px;
			margin: 10px 0;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
				margin-right: 10px;
			}

			.favi {
				width: 120px;
				border-left: solid 1px gray;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
			}
		}

		.transportPrice {
			font-size: 12px;
			color: gray;
			margin-top: 10px;
		}

		.goods_nav {
			position: fixed;
			/* 固定定位 */
			bottom: 0;
			/* 距离底部为0 */
			left: 0;
			/* 距离左边为0 */
			width: 100%;
			/* 宽度为100% */
			z-index: 100;
			/* 确保导航栏在最上层 */
			background-color: white;
			/* 给背景加一个颜色，避免底部透明 */
		}

	}
</style>